package com.hzhk.happyrent;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.StrictMode;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;

import com.hzhk.happyrent.network.API;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * User: hzhk(1097282555@qq.com)
 * Date: 2017/11/21
 * Time: 09:44
 * progject: HappyRent
 * Describe： ${Describe}
 */
public class App extends Application {
    private static App instantce;
    public static Context getContext() {
        return instantce;
    }
    private static Map<String,Activity> destoryMap = new HashMap<>();

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onCreate() {
        super.onCreate();
        instantce = this;
        setAppInfo();
        // android 7.0系统解决拍照的问题
        judgeVersion();
        UMShareAPI.get(this);
    }

    //各个平台的配置，建议放在全局Application或者程序入口
    {
        Config.DEBUG = true;
        PlatformConfig.setWeixin("wx08e1ba509b4f07ea", "ad85afb4403dd533bbdbf1edc4b7167d");
        PlatformConfig.setQQZone("1111111", "11111111");
        PlatformConfig.setSinaWeibo("111111", "1111111", "");
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void judgeVersion() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            builder.detectFileUriExposure();
        }
    }

    private void setAppInfo() {
        try {
            PackageInfo info = this.getPackageManager().getPackageInfo(
                    this.getPackageName(), 0);
//            ApplicationInfo appInfo = this.getPackageManager()
//                    .getApplicationInfo(getPackageName(),
//                            PackageManager.GET_META_DATA);
            API.VERSIONNAME = info.versionName + "";
            API.VERSIONCODE = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
        }
    }
    public static void addDestoryActivity(Activity activity,String activityName) {
        destoryMap.put(activityName,activity);
    }
    /**
     *销毁指定Activity
     */
    public static void destoryActivity(String activityName) {
        if(destoryMap.get(activityName)!=null){
            Activity activity = destoryMap.get(destoryMap);
            activity.finish();
        }
    }
    /**
     *销毁指定Activity
     */
    public static void destoryActivity() {
        Set<String> keySet=destoryMap.keySet();
        for (String key:keySet){
            destoryMap.get(key).finish();
        }
    }
    public static Activity getActivity(String activityName){
        if(destoryMap.get(activityName)!=null){
            Activity activity = destoryMap.get(activityName);
            return activity;
        }
        return null;
    }
}
